import { Component, OnInit } from '@angular/core';
import { environment } from "../../../../../../environments/environment";
import * as _ from "lodash";
import { SearchRemindService } from './search-remind.service';
import { DataService } from '../../../../../services/data.service'
import { Util } from '../../../../../common/util';
@Component({
  selector: 'app-search-remind',
  templateUrl: './search-remind.component.html',
  styleUrls: ['./search-remind.component.scss'],
  providers: [SearchRemindService]
})
export class SearchRemindComponent implements OnInit {
  // 角色下拉框数据
  public roleList: any = [];
  //用户列表全选按钮
  public userCheckedAll: any;
  //群组列表全选按钮
  public groupCheckedAll: any;
  //用户列表
  public userList: any = [];
  //群组列表
  public groupList: any = [];

  // 查询用户列表得到的数据（用户名）
  public finalData: any;
  //获取用户列表getBody
  public getUserBody: any = {
    pageIndex: 0,
    pageSize: 0,
    sRealname: '',//用户名
    sDeptname: '',//部门
    sRolename: ''//角色
  };

  //获取群组信息getBody
  public getGroupBody: any = {
    pageIndex: 0,
    pageSize: 0,
  };

  constructor(
    public searchRemindService: SearchRemindService,
    public dataService: DataService
  ) { }

  ngOnInit() {
    this.getRoleList();
    this.userlist();
    this.grouplist();
  }
  //关闭modal
  hideModal() {
    window['$']('#searchRemind').modal('hide');
    window['$']('#search-remind').modal('hide');
    window['$']('#submitSearchRemind').modal('hide');
    window['$']('#routeRevokeSearchRemind').modal('hide');
    window['$']('#revokeSearchRemind').modal('hide');
    window['$']('#qdiiRemindSearchRemind').modal('hide');
  }
  //列表全选
  checkedAll(data, checkAllBox) {
    if (checkAllBox) {
      // 更新为全选
      _.forEach(data, item => {
        item.isChecked = true;
      });
    } else {
      // 更新为不全选
      _.forEach(data, item => {
        item.isChecked = false;
      });
    }
  }
  // 列表是否全选
  ctrlCheckAll(data, checkAllBtn) {
    let that = this;
    let checkList = [];
    if (data !== null) {
      _.forEach(data, item => {
        if (item.isChecked == true) {
          checkList.push(item);
        }
      });
      if (checkList.length == data.length && checkList.length != 0) {
        that.judgeFileType(checkAllBtn, true);
      } else {
        that.judgeFileType(checkAllBtn, false);
      }
    }
  }
  //判断全选按钮类型
  judgeFileType(checkAllBtn, isTrue) {
    switch (checkAllBtn) {
      // 用户列表
      case 'userCheckedAll':
        this.userCheckedAll = isTrue;
        break;
      //群组列表
      case 'groupCheckedAll':
        this.groupCheckedAll = isTrue;
        break;

    }
  }
  //用户列表
  userlist() {
    let that = this;
    this.getUserBody.sRealname = _.trim(this.getUserBody.sRealname);
    this.getUserBody.sDeptname = _.trim(this.getUserBody.sDeptname);
    this.getUserBody.sRolename = _.trim(this.getUserBody.sRolename);

    this.searchRemindService.getUserList(this.getUserBody).subscribe(data => {
      if (data) {
        this.userList = data.list;
        console.log(this.userList)
      }
    })

  }
  //群组信息列表
  grouplist() {
    let that = this;
    this.searchRemindService.getGroupList(this.getGroupBody).subscribe(data => {
      if (data) {
        this.groupList = data.list;
        console.log(this.groupList)
      }
    })

  }

  // 查询并发射数据到另一个modal的input
  saveData() {
    let groupCheckedItem = [];
    let memberDataRaw = [];
    let memberData = [];
    /**
  * 用户列表要传sUserName去保存进数据库，传sRealName展示，群组的传nGroupId
  保存进数据库，传sMbrName用来展示
  */
    let userCheckedItem = {
      sRealname: [], //for display
      sUsername: [], //for database
      nGroupId: [],  //for database
      sMbrName: []  //for display
    };
    _.forEach(this.userList, (item) => {
      if (item.isChecked) {
        console.log(item);
        userCheckedItem.sRealname.push(item.sRealname);
        userCheckedItem.sUsername.push(item.sUsername);
      }
    })
    _.forEach(this.groupList, (item) => {

      if (item.isChecked) {
        // console.log(item);
        groupCheckedItem.push(item.nGroupId)
        userCheckedItem.nGroupId.push(item.nGroupId);
      }
    })
    if (groupCheckedItem.length > 0) {
      this.searchRemindService.getGroupMember(groupCheckedItem).subscribe((data) => {
        if (data) {
          console.log(data)
          // 返回群组成员信息
          memberDataRaw = data;

          // 提取名字进数组
          _.forEach(memberDataRaw, (item) => {
            // memberData.push(item.sMbrName);
            userCheckedItem.sMbrName.push(item.sMbrName);
          })
          // userData = userCheckedItem.join(',');
          // 转成字符串并发射
          // let finalData =  memberData.join(',');
          // console.log('memberData');
          // console.log(memberData);
          // this.finalData = _.concat(memberData, userCheckedItem);
          // let finalDataString = this.finalData.join(',');
          // console.log(finalDataString);
          console.log('发射的值');
          console.log(userCheckedItem);
          this.dataService.reminderEmitter.emit(userCheckedItem);
          // this.dataService.reminderEmitter.emit(finalDataString);
          window['$']('#searchRemind').modal('hide');
          window['$']('#search-remind').modal('hide');
        }
      })
    } else {
      // this.finalData = userCheckedItem;
      // let finalDataString = this.finalData.join(',');
      // console.log(finalDataString);
      this.dataService.reminderEmitter.emit(userCheckedItem);
      window['$']('#searchRemind').modal('hide');
      window['$']('#search-remind').modal('hide');
    }
    // if (memberData.length > 0) {
    //   this.finalData = _.concat(memberData, userCheckedItem);
    // }
    // else {
    //   this.finalData = userCheckedItem;

    // }
    // console.log('finalData');
    // console.log(this.finalData);
    // let finalDataString = this.finalData.join(',');
    // console.log(finalDataString);
    // this.dataService.reminderEmitter.emit(finalDataString);
    // window['$']('#searchRemind').modal('hide');
  }
  // 获取角色下拉框
  getRoleList() {
    let that = this;
    this.roleList = [];
    let getBody: any = {
      pageIndex: 0,
      pageSize: 0,
    };
    this.searchRemindService.getRoleList(getBody).subscribe(data => {
      if (data) {
        console.log(data);
        _.forEach(data.list, item => {
          that.roleList.push({
            sItemKey: item.sRolename,
            sItemValue: item.sRolename
          })
        })
      }
      setTimeout(function () {
        window['$']('#roleSelect').selectpicker('destroy');
        Util.loadSelect(that.roleList, 'roleSelect');
      }, 1);
    })
  }
}
